package com.eightworld3.supermarket.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.eightworld3.supermarket.domain.CustomerTab;
import com.eightworld3.supermarket.mapper.CustomerTabMapper;
import com.eightworld3.supermarket.service.AssetsEnum;
import com.eightworld3.supermarket.service.CustomerTabService;
import com.eightworld3.supermarket.service.dto.CustomerTabDTO;
import com.eightworld3.supermarket.service.dto.add.CustomerTabAddDTO;
import com.eightworld3.supermarket.service.dto.result.ResponseResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author EightWorld3
 */
@Service("customerTabService")
public class CustomerTabServiceImpl extends ServiceImpl<CustomerTabMapper,CustomerTab> implements CustomerTabService {
    private final CustomerTabMapper customerTabMapper;

    /**
     * CustomerTabServiceImpl构造方法
     * @param customerTabMapper 客户账单Mapper
     */
    public CustomerTabServiceImpl(CustomerTabMapper customerTabMapper){
        this.customerTabMapper = customerTabMapper;
    }

    /**
     * 查询所有
     * @return 返回所有CustomerTab(客户账单)
     */
    @Override
    public List<CustomerTab> selectAll(){
        return customerTabMapper.selectList(null);
    }

    /**
     * 根据tabId查询CustomerTab
     * @param tabId ID号
     * @return 返回tabId对应的CustomerTab对象(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult selectByTabId(Integer tabId) {
        if (tabId == null) {
            return new ResponseResult(2, "null");
        }

        CustomerTab entity = customerTabMapper.selectById(tabId);
        if (entity == null) {
            return new ResponseResult(2, "没有该对象");
        }

        return new ResponseResult(1,"成功",entity);
    }

    /**
     * 新增CustomerTab
     * @param customerTabAddDTO CustomerTab新增DTO对象
     * @return 返回当前当次新增状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult addCustomerTab(CustomerTabAddDTO customerTabAddDTO){
        CustomerTab entity = new CustomerTab();

        // 数量大于零判定
        if (customerTabAddDTO.getWareNum() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "总数需大于0");
        }else {
            entity.setWareNum(customerTabAddDTO.getWareNum());
        }

        // 总价大于零判定
        if (customerTabAddDTO.getTotalPrice() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "总价需大于0");
        }else {
            entity.setTotalPrice(customerTabAddDTO.getTotalPrice());
        }

        // 默认时间
        if(customerTabAddDTO.getTabDate() == null){
            entity.setTabDate(new Date(System.currentTimeMillis()));
        }else {
            entity.setTabDate(customerTabAddDTO.getTabDate());
        }

        // 备注内容字符限制
        if (customerTabAddDTO.getRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setRemark(customerTabAddDTO.getRemark());
        }

        return new ResponseResult(1,"成功",customerTabMapper.insert(entity));
    }

    /**
     * 更新CustomerTab
     * @param customerTabDTO CustomerTab DTO对象
     * @return 返回当前当次更新状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult updateCustomerTab(CustomerTabDTO customerTabDTO){
        CustomerTab entity = new CustomerTab();

        // 修改对象存在判断
        CustomerTab existEntity = customerTabMapper.selectById(customerTabDTO.getTabId());
        if(existEntity == null){
            return new ResponseResult(2, "客户账单对象不存在");
        }else {
            entity.setTabId(customerTabDTO.getTabId());
    }

        // 数量大于零判定
        if (customerTabDTO.getWareNum() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "总数需大于0");
        }else {
            entity.setWareNum(customerTabDTO.getWareNum());
        }

        // 总价大于零判定
        if (customerTabDTO.getTotalPrice() <= AssetsEnum.BOUNDARY_VALUE_ZERO.value()){
            return new ResponseResult(2, "总价需大于0");
        }else {
            entity.setTotalPrice(customerTabDTO.getTotalPrice());
        }

        // 默认时间
        if(customerTabDTO.getTabDate() == null){
            entity.setTabDate(new Date(System.currentTimeMillis()));
        }else {
            entity.setTabDate(customerTabDTO.getTabDate());
        }

        // 备注内容字符限制
        if (customerTabDTO.getRemark().getBytes().length > AssetsEnum.REMARK_VARCHAR_LIMIT.value()){
            return new ResponseResult(2, "备注内容过多，需 <= 120Byte");
        }else {
            entity.setRemark(customerTabDTO.getRemark());
        }

        return new ResponseResult(1,"成功",customerTabMapper.updateById(entity));
    }

    /**
     * 根据tabId删除CustomerTab
     * @param tabId ID号
     * @return 返回当前当次删除状况(经ResponseResult包装类修饰)
     */
    @Override
    public ResponseResult deleteCustomerTabById(int tabId){
        int deleteNum= customerTabMapper.deleteById(tabId);
        if (deleteNum == 0) {
            return new ResponseResult(0, "对象不存在");
        }
        return new ResponseResult(1, "成功");
    }
}
